<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/html">
    <head>
        <title>Chat</title>
        <style>
            #message{
                margin-top:20px;
                border:1px solid gray;
                padding:20px;
                max-width: 500px;
            }
        </style>
    </head>
<body>

   Nick: <input type="text" id = "nickname"/>
   <button onclick="connectWebSocket()">Connect</button>
   <button onclick="closeWebSocket()">Close</button>

   <div id="message" title="rome">
   </div>

   </br>
   Msg: <input id="text" type="text" />
   ToUser: <input id="toUser" type="text"/>
   <button onclick="send()">Send</button>

</body>

<script type="text/javascript">
    var websocket = null;
    var nickname = null;
    function connectWebSocket() {
        //连接前校验是否有昵称
        nickname = document.getElementById("nickname").value;
        /*if(nickname === ""){
            alert("Please input nickname")
            return;
        }*/
        //判断当前浏览器是否支持WebSocket
        var json = {accountId:"50005330002", proposalId:"KjIFUBPlUOPTjfkspb9yTQ", wxUserId:"1036440976689725440", avatar:"http://sasdad.jpg"+nickname, name:nickname+'1'};
        var str = encodeURIComponent(JSON.stringify(json));
        if('WebSocket' in window){
            //websocket = new WebSocket("ws://localhost:8084/ws/"+nickname+"?user="+str);
            websocket = new WebSocket("ws://84322-iyb-websocket.test.za-tech.net/ws/room"+"?param="+str);
            //websocket = new WebSocket("ws://localhost:8080/ws/room"+"?param="+str);
        } else{
            alert('Not support websocket')
        }

        //连接发生错误的回调方法
        websocket.onerror = function(){
            setMessageInnerHTML(">>> websocket error...");
        };

        //连接成功建立的回调方法
        websocket.onopen = function(event){
            setMessageInnerHTML(">>> websocket open...");
        }

        //接收到消息的回调方法
        websocket.onmessage = function(event){
            console.info(event.data);
            setMessageInnerHTML(event.data);
        }

        //连接关闭的回调方法
        websocket.onclose = function(){
            setMessageInnerHTML(">>> websocket close...");
        }

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function(){
            websocket.close();
        }
    }

    //将消息显示在网页上
    function setMessageInnerHTML(msg){
        var message = document.getElementById('message');
        message.innerHTML += msg + '<br/>';
    }

    //发送消息
    function send(){
        var message = document.getElementById('text').value;
        //设置消息接受者
        var toUser = document.getElementById("toUser").value;
        var webSocketMsg = {msg:message, toUser:toUser};
        if(toUser == ''){
            //群聊
            webSocketMsg.type = 0;
        } else {
            //单聊
            webSocketMsg.type = 1;
        }
        //websocket.send(nickname+ ":" +message);
        //json对象转换位json字符串
        websocket.send(JSON.stringify(webSocketMsg));
    }

    //关闭连接
    function closeWebSocket(){
        websocket.close();
    }
</script>
</html>